티스토리 뷰

# Composer 설치

# composer 설치해준다. php5.6 이상에서 사용할 수 있음.
composer require webonyx/graphql-php

# Core/Controller.php

class Graphql_Controller extends CI_Controller
{
    public function __construct() {
        parent::__construct();
    }

    public function response($schema=null) {
        try{
            $input = json_decode(file_get_contents('php://input'), true);
            $query = $input['query'];

            $variableValues = isset($input['variables']) ? $input['variables'] : null;
            $result = GraphQL\GraphQL::executeQuery(
                $schema,
                $query,
                $this->rootValue(),
                $context = null,
                $variableValues = null,
                $operationName = null,
                $fieldResolver = null,
                $myValiationRules
           );

           $output = $result->jsonSerialize();
        } catch (Exception $e) {
            $output = [
                'errors' => [
                    [
                        'message'   => $e->getMessage(),
                        'code'      => $e->getCode()
                    ]
                ]
            ];
        } finally {
            $this
                ->output
                ->set_content_type('json')
                ->set_output(json_encode($output));
        }
    }

    public function rootValue() {
        return [];
    }
}

# Controller.php

//ci config 설정안되었을 떄 autoload 추가.
//require_once 'vendor/autoload.php';

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Language\Parser;
use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\AST;

class Index extends Graphql_Controller
{
    public function __construct() {
        parent::__construct();
    }

    public function index(){
        //none
    }

    /*
    * 스키마들을 추가해준다.
    */
    public function rootValue() {
        return [
            'echo' => function($root, $args) {
                if (isset($args['message'])) {
                    return $args['message'];
                }

                return "no message";
            },
            'hello' => function($root, $args) {
                return "hello graphqlworld";
            }
        ];
    }

    public function graphql() {
        $document = Parser::parse(file_get_contents("schema.graphql"));
        $schema = BuildSchema::build($document);
        $this->response($schema);
    }
}

# schema.graphql

schema {
    query: Query,
    mutation: Mutation,
}

type Mutation {
    echo(message: String!): String,
    ...
}

type Query {
    hello: String,
    ...
}

# request

Query {
    mutation { echo(message: "This is Graphql") }
    query { hello }
}

 

참고: https://banjubu.tistory.com/117

 

[PHP] Codeigniter 에 GraphQL 적용하기

컴포저로 GraphQL을 추가해요. composer require webonyx/graphql-php 라우트 설정하고요. $route['graphql'] = '/graphql_ctl'; controllers 폴더에 Graphql_ctl.php를 만들어요.

banjubu.tistory.com

참고: https://github.com/webonyx/graphql-php

 

webonyx/graphql-php

A PHP port of GraphQL reference implementation. Contribute to webonyx/graphql-php development by creating an account on GitHub.

github.com

참고: https://webonyx.github.io/graphql-php/

 

graphql-php

About GraphQL GraphQL is a modern way to build HTTP APIs consumed by the web and mobile clients. It is intended to be an alternative to REST and SOAP APIs (even for existing applications). GraphQL itself is a specification designed by Facebook engineers. V

webonyx.github.io

참고 : https://graphql.org

 

GraphQL | A query language for your API

Evolve your APIwithout versions Add new fields and types to your GraphQL API without impacting existing queries. Aging fields can be deprecated and hidden from tools. By using a single evolving version, GraphQL APIs give apps continuous access to new featu

graphql.org

 

'공부합시다 > php' 카테고리의 다른 글

라라벨 EloquantModel Multiple PrimaryKey 이슈  (0) 2023.07.05
PHP L5-Swagger 예시  (0) 2023.06.15
array_count_values  (0) 2021.03.10
PHPRedis 설치하기  (0) 2021.02.16
Array를 Object로 변경하기  (0) 2020.05.20
댓글